package com.saxion.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.saxion.model.Bestelling;
import com.saxion.model.Product;


public class BestellingDaoImpl implements BestellingDao {
	
 private SessionFactory sessionFactory;
     
     public SessionFactory getSessionFactory() {return sessionFactory;}

     public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
     }


	@Override
	public List<Bestelling> getBestellingenVanKlant(String klant) {
		Session session = null;
		try{
			try{
				session = sessionFactory.getCurrentSession();
			}catch(HibernateException e){
				session = sessionFactory.openSession();
			}
			ArrayList<Bestelling> bestellingen = (ArrayList<Bestelling>) session.createQuery("from Bestelling where klantNaam = ?").setString(0, klant).list();
			return bestellingen;
		}catch(HibernateException e){
			session = sessionFactory.openSession();
		}finally{
			session.close();
		}
		return new ArrayList<Bestelling>();
	}


	@Override
	public Bestelling getBestellingById(int id) {
		Session session = null;
		try{
			try{
				session = sessionFactory.getCurrentSession();
			}catch(HibernateException e){
				session = sessionFactory.openSession();
			}
			ArrayList<Bestelling> bestellingen = (ArrayList<Bestelling>) session.createQuery("from Bestelling where bestel_id = ?").setLong(0, id).list();
			if(!bestellingen.isEmpty()){
				return bestellingen.get(0);
			}
		}catch(HibernateException e){
			session = sessionFactory.openSession();
		}finally{
			session.close();
		}
		return null;
	}


	@Override
	public int addBestelling(Bestelling b) {
		Session session = null;
		
        Transaction txD = null;
        try{
        	try{
    			session = sessionFactory.getCurrentSession();
    		}catch(HibernateException e){
    			session = sessionFactory.openSession();
    		}
        	if (session.getTransaction() != null
    	            && session.getTransaction().isActive()) {
    	        txD = session.getTransaction();
    	    } else {
    	        txD = session.beginTransaction();
    	    }
			session.save(b);
	        txD.commit();			
        }catch( HibernateException e){
        	if(txD !=null){
        		txD.rollback();
        	}
        	e.printStackTrace();
        } finally{
        	session.close();
        }
        
        return b.getBestelnummer();

	}


	@Override
	public void updateBestelling(Bestelling b) {
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}catch(HibernateException e){
			session = sessionFactory.openSession();
		}
        Transaction tx = null;
        try{
        	if (session.getTransaction() != null
    	            && session.getTransaction().isActive()) {
    	        tx = session.getTransaction();
    	    } else {
    	        tx = session.beginTransaction();
    	    }
			session.update(b);
	        tx.commit();			
        }catch( HibernateException e){
        	if(tx!=null){
        		tx.rollback();
        	}
        	e.printStackTrace();
        }finally{
        	session.close();
        }
	}


	@Override
	public boolean bevestigBestelling(Bestelling b) {
		try{
			b.setBevestigd(true);
			updateBestelling(b);
			return true;
		}catch(Exception e){
			return false;
		}
		
	}

}
